define(['egis'], function (egis) {
    Window.deserializer = Window.deserializer || new egis.core.Deserializer();
    Window.container = Window.container || {};
    dsBridge.register('create_ElementLayer', function (sourceId, layerId, name, zIndex) {
        var layer = new egis.carto.ElementLayer({
            "id": layerId,
            "name": name,
            "zIndex": zIndex
        });
        Window.container[sourceId] = layer;
    })

    dsBridge.register('ElementLayer_add', function (sourceId, elementId, content) {
        var elementLayer = Window.container[sourceId];
        var target = Window.container[elementId];
        var element = Window.deserializer.createJsInstance(JSON.parse(content));
        target.geometry = element.geometry;
        target.symbol = element.symbol;
        elementLayer.add(target);
    })

    dsBridge.register('ElementLayer_addElements', function (sourceId, targetId, isSingle) {
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        source.addElements(target, isSingle);
    })

    dsBridge.register('ElementLayer_addGElement', function (sourceId, element) {
        var elementLayer = Window.container[sourceId];
        element = Window.deserializer.createJsInstance(JSON.parse(element));
        elementLayer.add(element);
    })

    dsBridge.register('ElementLayer_addElementCollection', function (sourceId, elements, isSingle) {
        var elementLayer = Window.container[sourceId];
        var elementCollection = Window.deserializer.createJsInstance(JSON.parse(elements));
        elementCollection.reset();
        var index = elementCollection.getIdentityIndex();
        for (var i = 0, len = elementCollection.size(); i < len; ++i) {
            var element = elementCollection.get(i);
            var gElement = Window.container[element.id];
            gElement.properties = element.properties;

        }
        elementLayer.add(element);
    })

    dsBridge.register('ElementLayer_update', function (sourceId, targetId, isUpdateStyle, content) {
        var element = Window.deserializer.createJsInstance(JSON.parse(content));
        var source = Window.container[sourceId];
        var target = Window.container[targetId];
        target.geometry = element.geometry;
        target.symbol = element.symbol;
        source.update(target, isUpdateStyle);
    })

    dsBridge.register('layerVisible', function (layerId, visible) {
        var map = Window.egis_map;
        var layer = map.findLayer(layerId);
        if (!!layer)
            layer.visible = visible;
    })

    dsBridge.register('ElementLayer_setZIndex', function (sourceId, zIndex) {
        var source = Window.container[sourceId];
        source.setZIndex(zIndex);
    })

    dsBridge.register('ElementLayer_hitTest', function (sourceId, screenX, screenY) {
        try {
            var source = Window.container[sourceId];
            var graphic = source.hitTest(screenX, screenY);
            if (!!graphic && graphic.element) {
                var json = JSON.stringify(graphic.element);
                return json;
            }
        }
        catch (e) {
            alert(e);
        }
    })

    dsBridge.register('ElementLayer_hitTestList', function (sourceId, screenX, screenY) {
        var source = Window.container[sourceId];
        return source.hitTestList(screenX, screenY);
    })

    dsBridge.register('ElementLayer_select', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.select(graphic);
    })

    dsBridge.register('ElementLayer_unSelect', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.unSelect(graphic);
    })

    dsBridge.register('ElementLayer_remove', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        source.remove(element);
    })

    dsBridge.register('ElementLayer_showElement', function (sourceId, element, visible) {
        var elementLayer = Window.container[sourceId];
        element = Window.deserializer.createJsInstance(JSON.parse(element));
        elementLayer.add(element);
    })

    dsBridge.register('ElementLayer_selectEdit', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.selectEdit(graphic);
    })

    dsBridge.register('ElementLayer_unSelectEdit', function (sourceId, elementId) {
        var source = Window.container[sourceId];
        var element = Window.container[elementId];
        var graphic = new egis.carto.Graphic({
            layer: source,
            element: element
        });
        source.unSelectEdit(graphic);
    })

    dsBridge.register('ElementLayer_bringTop', function (sourceId, elementId) {
        var elementLayer = Window.container[sourceId];
        element = Window.container[elementId];
        elementLayer.bringTop(element);
    })

    dsBridge.register('ElementLayer_sendBottom', function (sourceId, elementId) {
        var elementLayer = Window.container[sourceId];
        element = Window.container[elementId];
        elementLayer.sendBottom(element);
    })
})